#ifndef UNIFIED_MEMORY_INTERFACES_H
#define UNIFIED_MEMORY_INTERFACES_H

#include <QObject>
#include <QImage>
#include <QString>
#include <QDateTime>
#include <memory>

// TDD: 测试驱动的接口设计

// 🎯 核心抽象：缓存项接口
class ICacheItem {
public:
    virtual ~ICacheItem() = default;
    virtual int getIndex() const = 0;
    virtual QImage getImage() const = 0;
    virtual size_t getMemorySize() const = 0;
    virtual QDateTime getLastAccessed() const = 0;
    virtual void markAccessed() = 0;
    virtual bool isLoaded() const = 0;
};

// 🎯 缓存加载服务接口
class ICacheLoadingService {
public:
    struct LoadResult {
        QImage image;
        bool success = false;
        QString errorMessage;
        QDateTime loadTime;
        size_t memoryUsed = 0;
    };
    
    virtual ~ICacheLoadingService() = default;
    virtual LoadResult loadFromCache(const QString& fileIdentity, int pageIndex) = 0;
    virtual bool isCacheAvailable(const QString& fileIdentity, int pageIndex) const = 0;
};

// 🎯 内存管理策略接口
class IMemoryStrategy {
public:
    struct EvictionDecision {
        QList<int> itemsToEvict;
        QList<int> itemsToPreload;
        QString reason;
        double confidence = 1.0;  // 决策置信度
    };
    
    virtual ~IMemoryStrategy() = default;
    virtual EvictionDecision makeEvictionDecision(
        const QList<ICacheItem*>& items,
        int currentFocus,
        size_t memoryLimit,
        size_t currentUsage,
        double memoryPressure) const = 0;
};

// 🎯 内存监控接口
class IMemoryMonitor {
public:
    virtual ~IMemoryMonitor() = default;
    virtual size_t getCurrentMemoryUsage() const = 0;
    virtual size_t getAvailableMemory() const = 0;
    virtual double getMemoryPressure() const = 0;
    virtual void startMonitoring() = 0;
    virtual void stopMonitoring() = 0;
};

// 🎯 性能分析接口
class IPerformanceProfiler {
public:
    struct OperationMetrics {
        QString operation;
        double avgTimeMs = 0.0;
        size_t avgMemoryDelta = 0;
        int callCount = 0;
        double successRate = 1.0;
    };
    
    virtual ~IPerformanceProfiler() = default;
    virtual void startOperation(const QString& operation) = 0;
    virtual void endOperation(const QString& operation, bool success = true) = 0;
    virtual OperationMetrics getMetrics(const QString& operation) const = 0;
    virtual void generateReport() = 0;
};

// 🎯 统一内存管理器接口
class IUnifiedMemoryManager {
public:
    enum class ContentType { MAIN_PAGE, THUMBNAIL, COMPOSED_PAGE };
    
    virtual ~IUnifiedMemoryManager() = default;
    virtual QImage getImage(ContentType type, int index, int currentFocus = -1) = 0;
    virtual void setCurrentFocus(ContentType type, int focus) = 0;
    virtual size_t getMemoryUsage(ContentType type) const = 0;
    virtual void registerPool(ContentType type, size_t maxMemoryBytes) = 0;
    virtual void setMemoryStrategy(ContentType type, std::unique_ptr<IMemoryStrategy> strategy) = 0;
};

// 🎯 测试用的Mock实现
class MockCacheLoadingService : public ICacheLoadingService {
private:
    mutable int m_callCount = 0;
    
public:
    LoadResult loadFromCache(const QString& fileIdentity, int pageIndex) override {
        m_callCount++;
        
        LoadResult result;
        result.image = QImage(100, 100, QImage::Format_RGB32);
        result.image.fill(Qt::blue);
        result.success = true;
        result.loadTime = QDateTime::currentDateTime();
        result.memoryUsed = result.image.sizeInBytes();
        
        return result;
    }
    
    bool isCacheAvailable(const QString& fileIdentity, int pageIndex) const override {
        return true;  // 简化的Mock实现
    }
    
    int getCallCount() const { return m_callCount; }
    void resetCallCount() { m_callCount = 0; }
};

class MockMemoryMonitor : public IMemoryMonitor {
private:
    size_t m_simulatedUsage = 0;
    size_t m_simulatedAvailable = 8ULL * 1024 * 1024 * 1024;  // 8GB
    
public:
    size_t getCurrentMemoryUsage() const override { return m_simulatedUsage; }
    size_t getAvailableMemory() const override { return m_simulatedAvailable; }
    double getMemoryPressure() const override { 
        return double(m_simulatedUsage) / double(m_simulatedUsage + m_simulatedAvailable);
    }
    void startMonitoring() override {}
    void stopMonitoring() override {}
    
    // 测试辅助方法
    void setSimulatedUsage(size_t usage) { m_simulatedUsage = usage; }
    void setSimulatedAvailable(size_t available) { m_simulatedAvailable = available; }
};

#endif // UNIFIED_MEMORY_INTERFACES_H
